<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['lang']){
	$_SESSION['show_lang']=$_RQ['lang'];
}
if($_RQ['op']=='main'){
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),array('avatar','nickname','credit1','credit2'));
	$goodsid = intval($_RQ['goodsid']);
	//返回链接
	$return['forward_url']=base64_encode('jy_xunichong/order.list//fromcreate=1');
	//下单页
	$item=array();
	//提交数据
	$post = array();
	if($goodsid){
		$_SESSION['goodsid']=$goodsid;
		//直接购买。
		$post['goodsid'] = $goodsid;
		$goods=pdo_get('jy_xunichong_goods',array('id'=>$goodsid));
		if(!$goods){
			exi('指定商品不存在！','error','goback');
		}
		$group=pdo_get('jy_xunichong_group',array('id'=>$goods['groupid']));
		$goods['image']=$goods['image']?tomedia($goods['image']):'';
		$goods['typetitle']=$this->gift_type[$goods['type']];
	}else{
		//从购物车购买
		exi('请选择商品！','error','jy_xunichong/index/');
	}
	//提交数据
	$post['gift_type'] = $goods['type'];
	$post['price'] = $goods['price'];
	$post['oldprice'] = $goods['oldprice'];
	$post['total'] = 1;
	$return['post']=$post;
	$return['goods']=$goods;
	$group=pdo_get('jy_xunichong_group',array('id'=>$goods['groupid'],'uniacid'=>$_SESSION['uniacid']));
	$return['group']=$group;
	set_html('title','确认订单');
	exi($return);
}
// 完成下单
if($_RQ['op']=='post'){
	$post=get_postdata();
	$price=$post['price']*$post['total'];
	$new_order=array(
		'mid'=>$_SESSION['uid'],
		'price'=>$price,
		'oldprice'=>$post['oldprice'],
		'remark'=>trim($post['remark']),
		'goodsid'=>$post['goodsid'],
		'total'=>intval($post['total'])?:1,
		'gift_type'=>intval($post['gift_type']),
	);
	$order_id=$this->model('model')->create($new_order);
	$params=array(
		'id'=>$order_id,
		'title'=>'虚拟充值订单',
		'fee'=>$new_order['price'],
		'pay_type'=>'wechat',//可选，此项作用为指定支付方式
		'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
		'back_url'=>'jy_xunichong/order.create/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
		'success_url'=>'jy_xunichong/order.list/',
		'error_url'=>'jy_xunichong/order.list/',
	);
	$payid=cfc('pay')->create_pay_order($params);
	pdo_update('jy_xunichong_order',array('pay_id'=>$payid),array('id'=>$order_id));
	$res=array(
		'pay_id'=>$payid,
		'order_id'=>$order_id,
	);
	exi($res);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1){
		exi('支付失败。','error','jy_xunichong/order.list/');
	}
	$order=pdo_get('jy_xunichong_order',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_xunichong/order.list/');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['price'].'|'.$payresult['fee'],'error','jy_xunichong/order.list/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
	);
	$this->model('model')->order_pay($order,$params);
	exi('提交成功!');
}

?>